require("../util.js");
require("../component/components.js");
require("../component/cityChoice.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var chinaList=JSON.parse(sessionStorage.getItem("chinaList"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#applyBusiness",
    data:{
        type:"",
        isScreen:"",
        contacts:"",
        name:"",
        telephone:"",
        chinaList:{
            provinces:[],//省
            cities:[],//市
            areas:[]//县
        },//中国省市列表
        provinceId:"",
        provinceName:"",
        cityId:"",
        cityName:"",
        citiesList:[],//市列表
        areasList:[],//区列表
        regionName:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        this.getChinaList();
    },
    methods:{
        //后退
        getBack: function () {
            location.href="/happyi/setUp";
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //乐购-申请商家
        applyShop:function(){
            var that=this,data;
            if(!that.regionName){
                that.$refs.prompt.toast("所在地区不能为空！");
                return false;
            }
            if(!that.name){
                that.$refs.prompt.toast("店铺名字不能为空！");
                return false;
            }
            if(!that.contacts){
                that.$refs.prompt.toast("联系人不能为空！");
                return false;
            }
            if(!that.telephone){
                that.$refs.prompt.toast("手机号不能为空！");
                return false;
            }else{
                if (!/^1[345789]\d{9}$/.test(that.telephone.replace(/^\s+|\s+$/g, ""))) {
                    that.$refs.prompt.toast("请填写正确的手机号码");
                    return false;
                }
            }
            data={
                loginUid:userInfo.loginUid,
                name:that.name,
                contacts:that.contacts,
                telephone:that.telephone,
                provinceId:that.provinceId,
                cityId:that.cityId
            };
            this.ajax({url:"/happyi/applyShop",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    location.href="/happyi/applyBusiness?type=2"
                }else{
                    vm.$refs.prompt.toast("提交失败！")
                }
            });
        },
        getClose:function(type){
            if(type==1){
                this.name=""
            }else if(type==2){
                this.contacts=""
            }else if(type==3){
                this.telephone=""
            }
        },
        getIsScreen:function(num) {
            var that = this;
            that.isScreen = num;
        },
        //获取中国省市区列表
        getChinaList:function(){
            var that=this;
            if(chinaList){
                that.chinaList=chinaList;
            }else{
                this.ajax({url:"/selfMedia/getChinaList",data:{version:that.version},dataType:"json",method:"post"},function(b){
                    if(b.json.code==0&&b.json.data){
                        that.chinaList=b.json.data;
                        sessionStorage.setItem("chinaList",JSON.stringify(that.chinaList));
                        //console.log(that.chinaList)
                    }else{
                        vm.$refs.prompt.toast("获取省市列表失败！")
                    }
                });
            }
        },
        //获取市
        getCity:function(code,name){
            console.log(code);
            var that=this;
            that.citiesList=[];
            that.provinceId=code;
            that.provinceName=name;
            that.chinaList.cities.forEach(function(item,index){
                if(item.provinceId==code){
                    that.citiesList.push(item);
                }
            });
        },
        choiceList:function(data){
            console.log(data)
            this.cityId=data.code;
            this.cityName=data.name;
            this.regionName=this.provinceName+'-'+this.cityName;
            this.getIsScreen();
        },
        //跳转页面
        getNext:function(type){
            if(type==1){
                location.href="/purchase/collageInfo?collageOrderId="+this.collageOrderId;
            }
        }
    },
    watch:{

    }
});
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
